package control;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.ServicoService;
import utils.Constantes;
import utils.Utils;

import model.Servico;

public class ServicoControl extends GenericControl implements IControl {

	
	private static final long serialVersionUID = 1L;

	@Override
	public void executar(HttpServletRequest request,
			HttpServletResponse response) {
		this.setRequest(request);
		this.setResponse(response);
		String method = request.getParameter("acao");
		if (method.equals("adicionar")) {
			this.adicionar();
		}else if (method.equals("openadicionar")) {
			this.openAdicionar();
		}else if (method.equals("pesquisar")) {
			this.pesquisar(request, response);
		}else if (method.equals("openpesquisar")) {
			this.openPesquisar();
		}else if (method.equals("excluir")) {
			this.excluir();
		}
		Dispatcher.showScreen(this.getRequest(), this.getResponse());
		
	}

	private void openPesquisar() {
		ArrayList<Servico> servicos = null;
		
//		int paginaAtual=1;
//		int count=0;
		
		ServicoService service = new ServicoService();
		servicos = service.select(new Servico(), this.getPaginacao().getPaginaAtual());
		this.getPaginacao().setCount(service.getCount());
		
		
		this.setAtributoRequest(Constantes.SERVICOS, servicos);
		this.setAtributoRequest("paginaAtual", this.getPaginacao().getPaginaAtual());
		this.setAtributoRequest("count", this.getPaginacao().getCount());
		
		////Dispatcher.showScreen(request, response);
	}

	private void openAdicionar() {
		// TODO Auto-generated method stub
		////Dispatcher.showScreen(request, response);
	}

	private void excluir() {
		Servico servico=this.loadActionToModel();
		
		ServicoService service = new ServicoService();
		service.delete(servico, this.getLogado());
		
		this.setAtributoRequest(Constantes.MENSAGEM, service.getResultado());
		this.openPesquisar();
	}

	private void pesquisar(HttpServletRequest request, HttpServletResponse response) {
		ArrayList<Servico> servicos = null;
		Servico servicoBusca=this.loadActionToModel();
		
//		int paginaAtual=1;
//		int count=0;
//		
//		if (!Utils.isBlank(request.getParameter("paginaAtual")))
//			paginaAtual=Integer.valueOf(request.getParameter("paginaAtual"));			
		
		ServicoService service = new ServicoService();
		servicos = service.select(servicoBusca, this.getPaginacao().getPaginaAtual());
		this.getPaginacao().setCount(service.getCount());
		
		
		request.setAttribute(Constantes.SERVICOS, servicos);
		request.setAttribute("servicoBusca", servicoBusca);
		
		this.setAtributoRequest("paginaAtual", this.getPaginacao().getPaginaAtual());
		this.setAtributoRequest("count", this.getPaginacao().getCount());
		
		////Dispatcher.showScreen(request, response);
	}

	private void adicionar() {
		Servico servico=this.loadActionToModel();
		
		ServicoService service = new ServicoService();
		service.insert(servico, this.getLogado());
		
		this.setAtributoRequest(Constantes.MENSAGEM, service.getResultado());		
		////Dispatcher.showScreen(request, response);
		
	}
	private Servico loadActionToModel() {
		HttpServletRequest request = this.getRequest();
		Servico servico=new Servico();
		if (!Utils.isBlank(request.getParameter(Constantes.IDSERVICO)))
			servico.setId(Integer.valueOf(request.getParameter(Constantes.IDSERVICO)));
		
		if (!Utils.isBlank(request.getParameter(Constantes.DESCRICAO)))
			servico.setDescricao(request.getParameter(Constantes.DESCRICAO));
		
		if (!Utils.isBlank(request.getParameter(Constantes.TIPOPRECO)))
			servico.setTipopreco(request.getParameter(Constantes.TIPOPRECO));
		
		if (!Utils.isBlank(request.getParameter(Constantes.TAMANHO)))
			servico.setTamanho(request.getParameter(Constantes.TAMANHO));
		
		
		if (!Utils.isBlank(request.getParameter(Constantes.VOLUMOSO)))
			servico.setVolumoso(request.getParameter(Constantes.VOLUMOSO));
		
		
		if (!Utils.isBlank(request.getParameter(Constantes.PRECO)))
			servico.setPreco(Float.valueOf(request.getParameter(Constantes.PRECO)));
		
		return servico;
		
	}

	

}
